<template>
	<view class="content b-t">
		<view class="isNo" v-if="addressList.length===0">还没有入住人哦，快新建一个吧！</view>
		<view  class="list b-b" v-else v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
			<uni-swipe-action :options="options" class="goFix" @click="bindClick($event,item.id)">
				<view class="cont goFix">
					<view class="add_user"><text class="name">{{item.name}}</text></view>
					<view class="wrapper">
						<view class="address-box">
							<text v-if="item.defaultStatus==1" class="tag">默认</text>
							<text class="address">{{item.phoneNumber}}</text>
						</view> 
						<view class="u-box">
							<text class="mobile">{{item.idCard}}</text>
						</view>
						<view class="u-box">
							<text class="mobile">{{item.province}} {{item.city}} {{item.region}} {{item.detailAddress}}</text>
						</view>
					</view>
					<text class="yticon icon-bianji" @click.stop="addAddress('edit', item)"></text>
					<text v-if="false" class="yticon icon-iconfontshanchu1" @click.stop="delAddress(item.id)"></text>
				</view>

			</uni-swipe-action>
		</view>
		<!--<text style="display:block;padding: 16upx 30upx 10upx;lihe-height: 1.6;color: #fa436a;font-size: 24upx;">-->
			<!--重要：添加和修改地址回调仅增加了一条数据做演示，实际开发中将回调改为请求后端接口刷新一下列表即可-->
		<!--</text>-->
		
		<button class="add-btn" @click="addAddress('add')">新建入住人</button>
	</view>
</template>

<script>
    import {_allUser,_delAddress} from '../../common/vmeitime-http/address.js'
    import uniSwipeAction from "@/components/uni-swipe-action/uni-swipe-action.vue"
	export default {
        components: {uniSwipeAction},
		data() {
			return {
                options: [{
                    text: 'del',
                    style: {
                        backgroundColor: '#ffffff'
                    }
                }],
				source: 0,
				addressList: [
//					{
//						name: '刘晓晓',
//						mobile: '18666666666',
//						addressName: '贵族皇仕牛排(东城店)',
//						address: '北京市东城区',
//						area: 'B区',
//						default: true
//					},{
//						name: '刘大大',
//						mobile: '18667766666',
//						addressName: '龙回1区12号楼',
//						address: '山东省济南市历城区',
//						area: '西单元302',
//						default: false,
//					}
				]
			}
		},
		onLoad(option){
			console.log(option.source);
			this.source = option.source;
			this.loadData()
		},
		methods: {
			//选择地址
			checkAddress(item){
				if(this.source == 1){
					//this.$api.prePage()获取上一页实例，在App.vue定义
//					this.$api.prePage().addressData = item;
					// this.$api.prePage().defaultUser = item;
					this.$api.prePage().qLogistics(item);
					uni.navigateBack()
				}
			},
			addAddress(type, item){ 
				uni.navigateTo({
					url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(item)}`
				})
			},
            async delAddress(id){
                let deladdr = await _delAddress({id:id})
				if(deladdr.data.code==200){
                    msg(`地址删除成功`);
                    this.loadData()
                }
			},
			//添加或修改成功之后回调
			refreshList(data, type){
				//添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
				this.addressList.unshift(data);
				
				console.log(data, type);
			},
            async loadData(id) {
                let addressList = await _allUser();
				this.addressList = addressList.data.data;
            },
            async bindClick(event,id){
			    console.log(event,id)
                uni.showModal({
                    content: '确定删除该地址吗？',
                    success: async (e)=>{
                        if(e.confirm){
                            let deladdr = await _delAddress({id:id})
                            if(deladdr.data.code==200){
                                this.$api.msg(`地址删除成功`);
                                this.loadData()
                            }
                        }
                    }
                })

			}
		}
	}
</script>

<style lang='scss'>
	page{
		padding-bottom: 120upx;
	}
	.content{
		position: relative;
	}
	.list{
		display: flex;
		/*align-items: center;*/
		padding: 20upx 30upx;;
		background: #fff;
		position: relative;
	}
	.wrapper{
		display: flex;
		flex-direction: column;
		flex: 1;
	}
	.isNo{
		color: $font-color-dark;
		font-size: 26upx;
		text-align: center;
		padding: 60upx 0;
	}
	.add_user{
		display: flex;
		font-size: 28upx;
		/*padding: 0 60upx 0 0;*/
		line-height: 40upx;
		width: 150upx;
	}
	.address-box{
		display: flex;
		align-items: center;
		.tag{
			font-size: 24upx;
			color: $uni-color-primary;
			margin-right: 10upx;
			background: #EEF4EE;
			/*<!--border: 1px solid $uni-color-primary;-->*/
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}
		.address{
			font-size: 24upx;
			color: $font-color-dark;
		}
	}
	.u-box{
		font-size: 24upx;
		color: $font-color-light;
		margin-top: 10upx;
		.name{
			margin-right: 30upx;
		}
	}
	.icon-bianji{
		display: flex;
		align-items: center;
		height: 135upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}
	
	.add-btn{
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 16upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: $uni-color-primary;
		border-radius: 10upx;
		/*box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);		*/
	}
	.icon-iconfontshanchu1{
		/*display: block;*/
		width: 100upx;
		height: 100%;
		color: red;
		font-size: 46upx;
		display: flex;
		justify-content: center;
		margin-top: -5upx;
		align-items: center;

	}
	.goFix{
		display: flex;
		width: 100%;
	}
</style>
